package org.timeticker.gui;

import org.apache.log4j.Logger;
import org.timeticker.*;

import javax.swing.event.TableModelEvent;
import java.util.ResourceBundle;

/**
 * Activity table model
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 11-Jul-2007 : 9:25:31 PM
 */
public class ActivityListTableModel extends ObjectListTableModel<Activity> implements ActivityListener {
  // Error prefix DATM00003

  /** Component logger */
  protected Logger logger = Logger.getLogger(this.getClass().getName());

  /**
   * Create a new Activities table model.
   *
   * @param activities activity list to construct table model for
   */
  public ActivityListTableModel(ActivityList activities) {
    super(activities);
    if (null == activities) throw new IllegalArgumentException("Activity list must not be null");
    // get resource bundle
    ResourceBundle rb = ResourceBundle.getBundle("org.timeticker.timeticker");
    // create a column list
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.Day.Title"), Day.class, "day"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.Title"), String.class, "title"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.WorkType.Title"), WorkTypeCode.class, "workTypeCode"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.Project.Title"), ProjectCode.class, "projectCode"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.StartTime.Title"), Time.class, "startTime"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.FinishTime.Title"), Time.class, "finishTime"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.Duration.Title"), Duration.class, "duration"));
    columns.add(new ObjectListTableModelColumn(rb.getString("Activity.Comments.Title"), String.class, "comments"));
    // add this model as activity listener
    activities.addActivityListener(this);
  }

  /**
   * Return activities list
   *
   * @return activities list
   */
  public final ActivityList getActivities() {
    return (ActivityList) getObjects();
  }

  /**
   * Set activities list
   *
   * @param activities activities list
   */
  public void setActivities(ActivityList activities) {
    // remove us as activity listener
    ((ActivityList)getObjects()).removeActivityListener(this);
    // add us as activity listener
    activities.addActivityListener(this);
    // set new objects
    setObjects(activities);    
    // fire table model event
    fireTableModelEvent(new TableModelEvent(this));
  }

  public void activityAdded(Activity activity) {
    fireTableModelEvent(new TableModelEvent(this));
  }

  public void activityRemoved(Activity activity) {
    fireTableModelEvent(new TableModelEvent(this));
  }

  public void activityStarted(Activity activity) {
    fireTableModelEvent(new TableModelEvent(this));
  }

  public void activityStopped(Activity activity) {
    fireTableModelEvent(new TableModelEvent(this));
  }

}
